package com.wkq.main.ui

import android.Manifest
import android.content.Context
import android.content.Intent
import android.location.Location
import android.util.Log
import android.widget.Toast
import com.wkq.base.activity.BasicVbActivity
import com.wkq.base.util.showToast
import com.wkq.main.databinding.ActivityLocationShowBinding
import com.wkq.main.databinding.ActivityMainBinding
import com.wkq.main.ui.kotlin.BatteryOptimizationUtil
import com.wkq.main.ui.kotlin.KotlinActivity
import com.wkq.util.DeviceInfoUtils
import com.wkq.util.NativeLocationHelper
import com.wkq.util.NativeLocationSingleHelper
import com.wkq.util.setSafeClickListener

/**
 *
 *@Author: wkq
 *
 *@Time: 2025/8/25 9:22
 *
 *@Desc:
 */
class LocationShowActivity: BasicVbActivity<ActivityLocationShowBinding>() {

    companion object{
      fun  startActivity(context: Context){
          context.startActivity(Intent(context,LocationShowActivity::class.java))
        }
    }
    var locationSingleHelper: NativeLocationSingleHelper? = null
    private val callback: (location: Location?) -> Unit = { location ->

        binding.tvLocation.text =
            "类型: ${location?.provider} " + "纬度: ${location?.latitude}, 经度: ${location?.longitude}"
    }
    val permissions = mutableListOf<String>(
        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
    )
    override fun initView() {
        var brand = DeviceInfoUtils.getBrand()
        var model = DeviceInfoUtils.getModel()
        var version = DeviceInfoUtils.getAndroidVersion()
        var systemName = DeviceInfoUtils.getSystemName()
        var systemVersion = DeviceInfoUtils.getSafeSystemVersion()

        binding.tvName.text = "品牌: ${brand} - ${model}"
        binding.tvOs.text =  "系统: ${systemName}-${systemVersion}"
        binding.tvVersion.text = "版本: Android $version"




        hasRequest = isGranted(permissions)

        locationSingleHelper = NativeLocationSingleHelper(
            this, timeout = 5000L, callback = callback
        )

        if (hasRequest){
            locationSingleHelper?.startLocation()
        }
        binding.btLocation.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showSingleInit()
            locationSingleHelper?.startLocation()
        }
        binding.btLocationProcess.setSafeClickListener {
            LocationActivity.startActivity(this)
        }

    }

    override fun initData() {

    }

    fun requestLocation() {
        requestPermissionsLauncher(10010, permissions)
    }


    fun showSingleInit() {
        if (locationSingleHelper == null) {
            showToast("请先初始化")
        }
    }
    var hasRequest = false
    override fun authorized(permissionType: Int, permissionList: MutableList<String>) {
        if (permissionType == 10010) {
            hasRequest = true
        }
    }
}